Plotting data is fun. We also do it for data exploration (see the session tomorrow). Still, at least in this slot of exercise, we just want to familiarize ourselves with some of the graphic facilities in R.
So let’s start with a simple task: creating a simple scatter plot. For this purpose, we first load the GESIS Panel COVID-19 data.
library(dplyr)
library(haven)
gp_covid <-
read_sav(
"../data/ZA5667_v1-1-0.sav"
) %>%
sjlabelled::set_na(na = c(-1:-99, 97))
Now let’s plot an actual variable from the dataset.
jitter().
plot(gp_covid$political_orientation, gp_covid$hzcy001a)
# add some jitter
plot(
jitter(gp_covid$political_orientation, 2),
jitter(gp_covid$hzcy001a, 2)
)
Who doesn’t like some colors, right? This exercise is not for a print journal with ridiculous fees for beautiful colorful plots. Let’s use that to our advantage.
ColourPicker add-in for more modern colors?
plot(
jitter(gp_covid$political_orientation, 2),
jitter(gp_covid$hzcy001a, 2),
col = c("#1C86EE", "#FFFFFF", "#FFFFFF")
)
These scatterplots can get boring. What do you think about some bar plots? However, creating only one bar plot is for beginners, we can do more and plot more than once.
par() function in combination with its mfrow option.
tab_1 <- table(gp_covid$hzcy001a)
tab_2 <- table(gp_covid$hzcy002a)
tab_3 <- table(gp_covid$hzcy003a)
tab_4 <- table(gp_covid$hzcy004a)
par(mfrow = c(2, 2))
barplot(tab_1)
barplot(tab_2)
barplot(tab_3)
barplot(tab_4)
That’s interesting. One may wonder what the median is in each of these distributions. You know what’s perfect for visualizing this statistic? Boxplots!
boxplot().
par(mfrow = c(2, 2))
boxplot(gp_covid$hzcy001a)
boxplot(gp_covid$hzcy002a)
boxplot(gp_covid$hzcy003a)
boxplot(gp_covid$hzcy004a)
Before we later start with the other exercises, you may want to consider to clean your graphics device with dev.off()
dev.off()
## null device
## 1